package com.fanghaoran.service;

import com.fanghaoran.bean.Interaction;
import com.fanghaoran.dao.StudyInfoDao;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: win11
 * @Date: 2023/03/24/14:31
 * @Description: 单词数和学习时间的service层
 */
public class StudyInfoService {
    private StudyInfoDao studyInfoDao=new StudyInfoDao();
    /**
     *统计本次登录的学习时长,然后更新数据库中总的学习时长
     */
    public void updateStudyTime(Date beginTime,Date endTime,int userId) throws Exception {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始学习时间
        beginTime=sdf.parse(sdf.format(beginTime));
        //结束学习时间
        endTime=sdf.parse(sdf.format(endTime));
        //本次学习时长(单位:毫秒)
        long studyTime=endTime.getTime()-beginTime.getTime();
        //之前总学习时长(单位:分钟)
        int preTime=(int)getStudyTime(userId).getMessage();
        //新的总学习时长(注意时间单位的转化)
        long totalTime=preTime+studyTime/1000/60;
        //数据库存储操作
        studyInfoDao.updateStudyTime(totalTime,userId);
    }
    /**
     * 获取已学习时长
     */
    public Interaction getStudyTime(int userId) throws Exception {
        return studyInfoDao.getStudyTime(userId);
    }

    /**
     * 获取学习计划
     */
    public Interaction getStudyPlan(int userId) throws Exception {
        return studyInfoDao.getStudyPlan(userId);
    }

    /**
     * 更新学习计划
     */
    public void updateStudyPlan(int userId) throws SQLException {
        studyInfoDao.updateStudyPlan(userId);
    }

    /**
     *更新已学单词个数
     */
    public void updateWordAmount(int userId) throws Exception {
        studyInfoDao.updateWordAmount(userId);
    }
    /**
     *获取该用户的已学单词个数
     */
    public Interaction getWordAmount(int useId) throws Exception {
        return studyInfoDao.getWordAmount(useId);
    }
}
